#include "mission.h"

namespace IAlib
{

Mission::Mission(): m_name(""), m_importance(0), m_complete(false)
{
}

Mission::Mission(const Mission &mission)
{
    m_name = mission.m_name;
    m_importance = mission.m_importance;
    m_complete = mission.m_complete;
    for (int i = 0; i < mission.m_procedures.size(); i++)
    {
        addProcedure(*mission.m_procedures[i]);
    }
}

Mission::~Mission()
{
    for (int i = 0; i < m_procedures.size(); i++)
    {
        delete(m_procedures[i]);
    }
}

void Mission::addProcedure(const Procedure &procedure)
{
    Procedure * newProcedure = new Procedure(procedure);
    m_procedures.append(newProcedure);
}

void Mission::removeProcedure(const QString &name)
{
    for (int i = 0; i < m_procedures.size(); i++)
    {
        if (m_procedures[i]->getName() == name)
        {
            delete(m_procedures[i]);
            m_procedures.removeAt(i);
        }
    }
}


Procedure* Mission::getProcedure(const QString& name)
{
    bool exist = false;
    int i = 0;
    for (i = 0; i < m_procedures.size(); i++)
    {
        if (m_procedures[i]->getName() == name)
        {
            exist = true;
            break;
        }
    }
    if (exist)
    {
        return m_procedures[i];
    }
    else
    {
        return NULL;
    }
}

Procedure * Mission::getBestProcedure()
{
    if (!m_procedures.isEmpty())
    {
        int best_procedure_index = 0;
        for (int i = 1; i < m_procedures.size(); i++)
        {
            if (m_procedures[best_procedure_index]->getEfficiency() < m_procedures[i]->getEfficiency())
            {
                best_procedure_index = i;
            }
        }
        return m_procedures[best_procedure_index];
    }
    else
    {
        return NULL;
    }
}

QDomElement Mission::toXML(QDomDocument &doc)
{
    QDomElement ele = doc.createElement("mission");
    ele.setAttribute("name", m_name);
    ele.setAttribute("importance", m_importance);
    QDomElement procedures = doc.createElement("procedures");
    for (int i = 0; i < m_procedures.size(); i++)
    {
        procedures.appendChild(m_procedures[i]->toXML(doc));
    }
    ele.appendChild(procedures);
    return ele;
}

void Mission::fromXML(QDomElement ele)
{
    m_name = ele.attribute("name","");
    m_importance = ele.attribute("importance","").toInt();
    QDomNode n = ele.firstChild();
    while (!n.isNull())
    {
        QDomElement e = n.toElement();
        if (e.tagName() == "procedures")
        {
            QDomNode procedureNode = e.firstChild();
            while (!procedureNode.isNull())
            {
                Procedure procedure;
                procedure.fromXML(procedureNode.toElement());
                addProcedure(procedure);
                procedureNode = procedureNode.nextSibling();
            }
        }
        n = n.nextSibling();
    }
}

Mission& Mission::operator=(const Mission& mission)
{
    m_name = mission.m_name;
    m_importance = mission.m_importance;
    m_complete = mission.m_complete;
    for (int i = 0; i < m_procedures.size(); i++)
    {
        delete(m_procedures[i]);
    }
    m_procedures.clear();
    for (int i = 0; i < mission.m_procedures.size(); i++)
    {
        addProcedure(*mission.m_procedures[i]);
    }
    return *this;
}

}
